///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_GUI_COCOA_CCOCOAFDPOLLER_H_
#define _BUOLA_GUI_COCOA_CCOCOAFDPOLLER_H_

#include "auto.h"
#include <buola/io/cfdpoller.h>

namespace buola { namespace gui { namespace cocoa {

///\addtogroup io
///@{

/////////////////////////////////////////////////////////////////////////////

/// select fd poller

/// fd poller which uses select as polling mechanism
///
/////////////////////////////////////////////////////////////////////////////

class CCocoaFDPoller : public io::CFDPoller
{
private:
    //! file-descriptor info
    struct SFD
    {
        io::CFDListener *mListener;
        int mFD;
        io::ENotify mType;
        void *mClosure;
    };
    
public:
//! \name constructor and destructor
//@{
    //! constructor

    //! creates a poller and doesn't link it to any event loop
    CCocoaFDPoller();

    //! constructor

    //! creates a poller and links it to the given event loop
    CCocoaFDPoller(CEventLoop &pLoop);

    //! destructor
    virtual ~CCocoaFDPoller();
//@}

public:
    //! adds a file-descriptor to listen to
    virtual void AddFDListener(io::CFDListener*,int pFD,io::ENotify pEvents,void *pClosure=nullptr,
                       io::ENotifyOp pOp=io::NOTIFYOP_SET);

    //! removes all FD listeners for a certain file descriptor
    virtual void RemoveFDListeners(int pFD);

    //! removes all FD listeners for a certain listener
    virtual void RemoveFDListeners(io::CFDListener *pListener);

    //! returns true if there was activity in any of them
    virtual bool WaitFDs(const chrono::nanoseconds &pDuration);

private:
    void UpdateSets();
    
private:
    std::vector<std::unique_ptr<SFD>> mFDs;
    
    fd_set mReadFDSet;
    fd_set mWriteFDSet;
    fd_set mExcFDSet;
    int mMaxFD;
};

///@}

/*namespace cocoa*/ } /*namespace gui*/ } /*namespace buola*/ }

#endif
